package com.chendx.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.behavior.mapper.ApBehaviorEntryMapper;
import com.chendx.behavior.service.ApBehaviorEntryService;
import com.chendx.model.behavior.pojos.ApBehaviorEntry;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by chendx on 2022/3/8.
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements ApBehaviorEntryService {

    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        if (userId != null){
            ApBehaviorEntry one = getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, userId).eq(ApBehaviorEntry::getType, 1));
            if (one == null){
                one = new ApBehaviorEntry();
                one.setEntryId(userId);
                one.setCreatedTime(new Date());
                one.setType((short) 1);
                save(one);
            }
            return one;
        }
        //根据设备id查询行为实体
        if(userId == null && equipmentId != null && equipmentId != 0){
            ApBehaviorEntry one = getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, equipmentId).eq(ApBehaviorEntry::getType, 0));
            if (one == null){
                one = new ApBehaviorEntry();
                one.setEntryId(userId);
                one.setCreatedTime(new Date());
                one.setType((short) 0);
                save(one);
            }
            return one;
        }
        return null;


    }
}
